 package com.oanda.blackberry.restapi;
 
 import com.oanda.blackberry.net.ConnectionManager;
 import com.oanda.blackberry.net.RestException;
 import com.oanda.blackberry.net.UrlEncoder;
 import com.oanda.blackberry.util.log.Logger;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import javax.microedition.io.HttpsConnection;
 
 public class Trade extends Rest
 {
   public static long maxTradeId = 0L;
 
   public String order(String sessionToken, String symbol, String units, String accountid, String type, String price, String lowPrice, String highPrice, String stopLoss, String takeProfit, String trailingStop)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"ids\":[12345,12346,12347,12348],\"symbol\":\"EUR/USD\",\"units\":150,\"price\":1.2345,\"margin_used\":12000.0,\"dir\":\"L\"}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/trade/open.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + ((symbol != null) && (symbol.length() > 0) ? "&symbol=" + UrlEncoder.encode(symbol, "") : "") + ((units != null) && (units.length() > 0) ? "&units=" + UrlEncoder.encode(units, "") : "") + ((accountid != null) && (accountid.length() > 0) ? "&account_id=" + UrlEncoder.encode(accountid, "") : "") + ((type != null) && (type.length() > 0) ? "&type=" + UrlEncoder.encode(type, "") : "") + ((price != null) && (price.length() > 0) ? "&price=" + UrlEncoder.encode(price, "") : "") + ((lowPrice != null) && (lowPrice.length() > 0) ? "&low_price=" + UrlEncoder.encode(lowPrice, "") : "") + ((highPrice != null) && (highPrice.length() > 0) ? "&high_price=" + UrlEncoder.encode(highPrice, "") : "") + ((stopLoss != null) && (stopLoss.length() > 0) ? "&stop_loss=" + UrlEncoder.encode(stopLoss, "") : "") + ((takeProfit != null) && (takeProfit.length() > 0) ? "&take_profit=" + UrlEncoder.encode(takeProfit, "") : "") + ((trailingStop != null) && (trailingStop.length() > 0) ? "&trailing_stop=" + UrlEncoder.encode(trailingStop, "") : "");
     }
     catch (IOException i)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     Logger.log(url);
     Logger.log("connecting");
 
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("trade.order: " + this.response);
     return this.response;
   }
 
   public String list(String sessionToken, String accountId, String optMaxTradeId, String optMaxCount, String optSymbol, String optTradeIds)
     throws RestException
   {
     HttpsConnection connection = null;
     InputStream inputstream = null;
     OutputStream outputstream = null;
 
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"open_trades\" : [{ \"id\" : 12345, \"units\" : 5, \"dir\" : \"L\", \"symbol\" : \"EUR/USD\", \"time\" : 1234567890, \"price\" : 1.45123, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"trailing_stop\" : 10 },{ \"id\" : 12344, \"units\" : 100, \"dir\" : \"S\", \"symbol\" : \"EUR/USD\", \"time\" : 1234567891, \"price\" : 1.45123, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"trailing_stop\" : 10 },{ \"id\" : 789, \"units\" : 100, \"dir\" : \"S\", \"symbol\" : \"EUR/USD\", \"time\" : 1234567891, \"price\" : 1.45123, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"trailing_stop\" : 10 },{ \"id\" : 890, \"units\" : 100, \"dir\" : \"S\", \"symbol\" : \"EUR/USD\", \"time\" : 1234567891, \"price\" : 1.45123, \"stop_loss\" : 1.2, \"take_profit\" : 1.7, \"trailing_stop\" : 10 }]}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/trade/list.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + ((accountId != null) && (accountId.length() > 0) ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + ((optMaxTradeId != null) && (optMaxTradeId.length() > 0) ? "&max_trade_id=" + UrlEncoder.encode(optMaxTradeId, "") : "") + ((optMaxCount != null) && (optMaxCount.length() > 0) ? "&max_count=" + UrlEncoder.encode(optMaxCount, "") : "") + ((optSymbol != null) && (optSymbol.length() > 0) ? "&symbol=" + UrlEncoder.encode(optSymbol, "") : "") + ((optTradeIds != null) && (optTradeIds.length() > 0) ? "&trade_ids=" + UrlEncoder.encode(optTradeIds, "") : "");
 
       url = url + parameters;
     } catch (IOException e) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("trade.list: " + this.response);
 
     return this.response;
   }
 
   public String change(String sessionToken, String tradeId, String accountId, String optStopLoss, String optTakeProfit, String optTrailingStop)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "";
     }
     String url = ConnectionManager.getUrl() + "v1/trade/change.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (tradeId != null ? "&trade_id=" + UrlEncoder.encode(tradeId, "") : "") + ((optStopLoss != null) && (optStopLoss.length() > 0) ? "&stop_loss=" + UrlEncoder.encode(optStopLoss, "") : "") + ((optTakeProfit != null) && (optTakeProfit.length() > 0) ? "&take_profit=" + UrlEncoder.encode(optTakeProfit, "") : "") + ((optTrailingStop != null) && (optTrailingStop.length() > 0) ? "&trailing_stop=" + UrlEncoder.encode(optTrailingStop, "") : "");
     }
     catch (IOException e)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("trade.change response: " + this.response);
     return this.response;
   }
 
   public String poll(String sessionToken, String accountId, String maxTradeId)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"closed\" : [10000, 10001],\"updated\" : [10002],\"opened\" : [12347],\"max_trade_id\" : 12349}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/trade/poll.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (maxTradeId != null ? "&max_trade_id=" + UrlEncoder.encode(maxTradeId, "") : "");
 
       url = url + parameters;
     }
     catch (IOException e) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("trade.poll response: " + this.response);
     return this.response;
   }
 
   public String trailingAmount(String sessionToken, String accountId, String symbols)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"trailing_amounts\" : [{ \"id\" : 1, \"symbol\" : \"EUR/USD\", \"serverTime\" : 1234567890, \"ta\" : 10 },{ \"id\" : 2, \"symbol\" : \"EUR/USD\", \"serverTime\" : 1234567890, \"ta\" : 100 },{ \"id\" : 3, \"symbol\" : \"USD/CAD\", \"serverTime\" : 1234567890, \"ta\" : 15 }]}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/trade/trailing_amount.json";
     try {
       String parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (symbols != null ? "&symbols=" + UrlEncoder.encode(symbols, "") : "");
 
       url = url + parameters;
     }
     catch (IOException e) {
     }
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url);
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("trailingAmount response: " + this.response);
     return this.response;
   }
 
   public String close(String sessionToken, String tradeId, String accountId, String optPrice)
     throws RestException
   {
     if (ConnectionManager.getUrl().equals("device")) {
       return "{\"id\" : 12346,\"price\" : 1.2345,\"profit\" : 10.0,\"symbol\" : \"EUR/USD\",\"dir\" : \"L\"}";
     }
 
     String url = ConnectionManager.getUrl() + "v1/trade/close.json";
     String parameters = "";
     try {
       parameters = "?session_token=" + UrlEncoder.encode(sessionToken, "") + (accountId != null ? "&account_id=" + UrlEncoder.encode(accountId, "") : "") + (tradeId != null ? "&trade_id=" + UrlEncoder.encode(tradeId, "") : "") + ((optPrice != null) && (optPrice.length() > 0) ? "&price=" + UrlEncoder.encode(optPrice, "") : "");
     }
     catch (IOException e)
     {
     }
 
     url = url + ConnectionManager.getConnectionSuffix();
 
     this.response = proceedConnection(url, parameters.substring(1));
 
     if (this.error) {
       throw new RestException(this.errorMessage + (this.response == null ? "" : this.response));
     }
     Logger.log("trade.close response: " + this.response);
     return this.response;
   }
 
   public static void setMaxTradeId(long maxTradeId_)
   {
     maxTradeId = maxTradeId_;
   }
 
   public static long getMaxTradeId() {
     return maxTradeId;
   }
}

